package com.pos.clothes.controller;

import com.github.pagehelper.PageInfo;
import com.pos.clothes.mapper.OrderMapper;
import com.pos.clothes.pojo.*;
import com.pos.clothes.service.ClothesService;
import com.pos.clothes.service.DiscountService;
import com.pos.clothes.service.NormsService;
import com.pos.clothes.service.OrderService;
import com.pos.clothes.util.JsonData;
import com.pos.clothes.util.ValidFlag;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.Time;
import java.util.*;

@Controller
@RequiresPermissions(value = {"1","2"},logical = Logical.OR)
@RequestMapping("/Order")
public class OrderController {
    @Autowired
    public OrderService service;
    @Autowired
    public NormsService normsService;
    @Autowired
    public DiscountService discountService;
    @Autowired
    public Norms norms;
    @ResponseBody
    @RequestMapping("/getAllOrder")
    public Object getAllOrder(){
        return JsonData.buildSuccess(service.getAllOrder(),"ok");
    }
    @ResponseBody
    @RequestMapping("/getOrderDetail")
    public Object getOrderDetail(@RequestParam int idorder){
        return JsonData.buildSuccess(service.getOrderDetail(idorder),"ok");
    }
//    @RequestMapping("/getNormsByCode")
//    @ResponseBody
//    public Object getNormsByCode(@RequestParam(value = "code",required = false) String code,
//                                 @RequestParam(value = "name",required = false) String name,
//                                 @RequestParam(value = "flag",required = false) String flag){
//        Norms norms = new Norms();
//        Clothes clothes = new Clothes();
//
//        clothes.setName(name);
//        clothes.setFlag(flag);
//
//
//        norms.setCode(code);
//        norms.setClothes(clothes);
//        return service.searchNormByCode(norms);
//    }

    @RequestMapping("/getNormsByCode")
@ResponseBody
    public Object getNormsByCode(@RequestParam(value = "code") String code){
        Norms norms = new Norms();
        Clothes clothes = new Clothes();
        norms.setCode(code);
        norms.setClothes(clothes);
        List<Norms> list = null;
        if(code.length()==13){
            list = service.searchNormByCode(norms);
        }
        if(list!=null&&list.size()!=0){
            return list;
        }
        else{
            return JsonData.buildError("没有搜到数据,弹出窗口！");
        }
    }
        @RequestMapping("/getNormByNameFlag")
    @ResponseBody
    public Object getNormByNameFlag(@RequestParam(value = "name",required = false) String name,
                                 @RequestParam(value = "flag",required = false) String flag){
        Norms norms = new Norms();
        Clothes clothes = new Clothes();
        ///1
        clothes.setName(name);
        clothes.setFlag(flag);

        norms.setClothes(clothes);
        return service.searchNormByCode(norms);
    }


    @RequestMapping("/calculation")
    @ResponseBody
    public Object calculation(@RequestBody Map<String,Object> map){
        List<Object> list = (List<Object>) map.get("order_list");
        float pay = (int) map.get("pay");
        int iddiscount = (int)map.get("iddiscount");
        Map<String,Float> returnMap = service.calculation(list,pay,iddiscount);
        if (returnMap==null){
            return JsonData.buildError("支付金额不足，请核对交易");
        }
        return JsonData.buildSuccess(returnMap,"计算完成");
    }
    @RequestMapping("/finish")
    @ResponseBody
    public Object finish(@RequestBody Map<String,Object> map){
        List<Object> list = (List<Object>) map.get("order_list");
        int iduser = (int)map.get("iduser");
        float pay = (int) map.get("pay");
        int idkind = (int) map.get("idkind");
        int iddiscount = (int)map.get("iddiscount");
        Map<String,Float> returnMap;
        List<Norms> normsList = new ArrayList<>();
        for (int i =0;i<list.size();i++){
            Map<String,Integer> normObject = (Map<String, Integer>) list.get(i);
            Norms norms = new Norms();
            norms.setId(normObject.get("id"));
            norms.setRemains(normsService.count_getNormsRemain(norms)-normObject.get("num"));
            normsList.add(norms);
        }
        //更新库存
        int res = service.updateNorm(normsList);
        if (res!=1) {
            return JsonData.buildSuccess(res, "下单失败");
        }
        //更新order表
        returnMap = service.calculation(list,pay,iddiscount);
        Order order = new Order();
        order.setIddiscount(iddiscount);
        order.setIdkind(idkind);
        order.setIduser(iduser);
        order.setOriginalPrice(returnMap.get("sumPrice"));
        order.setTotal(returnMap.get("total"));
        order.setRepay(returnMap.get("reurnPay"));
        order.setPay(pay);
        order.setTime(new Date(System.currentTimeMillis()));
        order.setDeal("");
        order.setCount(normsList.size());
        res = service.insertOrder(order);
        //更新orderdetail表(构造列表)
        List<Order_detail> order_details = new ArrayList<>();
        for (int i=0;i<list.size();i++){
            Order_detail order_detail = new Order_detail();
            order_detail.setIdorder(res);
            Map<String,Integer> normObject = (Map<String, Integer>) list.get(i);
            order_detail.setIdnorm(normObject.get("id"));
            order_detail.setNumber(normObject.get("num"));
            order_detail.setNote("");
            order_detail.setKey(0);
            order_detail.setKey_discount(0);
            order_details.add(order_detail);
        }
        service.insertOrderDetails(order_details);
        return JsonData.buildSuccess();
    }
    @RequestMapping("/test")
    @ResponseBody
    public Object test(@RequestBody Map<String,Object> map){
        return JsonData.buildSuccess(map,"测试");
    }
}
